<?php

try {
	$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
	$key = $_GET["key"];

	if (is_null($lang) || empty($lang)) {
		throw new Exception('parameter lang is mandatory');
	}
	if (is_null($key) || empty($key)) {
		throw new Exception('parameter key for language '.$lang.' is mandatory');
	}

	if ($lang == "it") {
		include '../resources/Message_IT.php';
	} else if ($lang == "en") {
		include '../resources/Message_EN.php';
	} else {
		include '../resources/Message_EN.php';
	}
	
	$language = new Language();
	$value = $language->getLabel($key);
	
	if (is_null($value)) {
		throw new Exception('value for '.$key.' not found in '.$lang.' language');
	}
	
	$jsonResult['result'] = "OK";
	$jsonResult['language'] = $lang;
	$jsonResult['value'] = $value;

	print json_encode($jsonResult);


} catch(Exception $ex) {
	//Return error message
	$jsonResult = array();
	$jsonResult['result'] = "ERROR";
	$jsonResult['message'] = $ex -> getMessage();
	print json_encode($jsonResult);
}
?>